package com.kantboot.functional.third.party.wechat.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.kantboot.functional.third.party.wechat.service.ITpWechatService;
import com.kantboot.functional.third.party.wechat.setting.FunctionalThirdPartyWechatSetting;
import com.kantboot.functional.third.party.wechat.util.mp.login.Code2Session;
import com.kantboot.user.account.domain.vo.LoginVO;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class TpWechatServiceImpl implements ITpWechatService {

    @Resource
    private FunctionalThirdPartyWechatSetting setting;

//    @Resource
//    private ISysThirdPartyService thirdPartyService;

    private Code2Session.Result getCode2SessionByCode(String code){
        // 获取微信小程序appId
        String appid = setting.getWechatMpAppid();
        // 获取微信小程序appSecret
        String appSecret = setting.getWechatMpAppSecret();

        Code2Session.Param param = new Code2Session.Param();
        param.setAppid(appid);
        param.setSecret(appSecret);
        param.setJsCode(code);
        param.setGrantType("authorization_code");
        return Code2Session.getResult(param);
    }

    @Override
    public String getOpenIdByCodeInMp(String code) {
        return getCode2SessionByCode(code).getOpenid();
    }

    @Override
    public String getUnionIdByCodeInMp(String code) {
        return getCode2SessionByCode(code).getUnionid();
    }

    @Override
    public LoginVO loginInMp(String code) {
//        Code2Session.Result code2Session = getCode2SessionByCode(code);
//
//        ThirdPartyLoginDTO thirdPartyLoginDTOUnionid = new ThirdPartyLoginDTO()
//                .setThirdPartyCode("wechat")
//                .setKey("unionid")
//                .setValue(code2Session.getUnionid());
//
//        SysUserThirdParty by = thirdPartyService.get(thirdPartyLoginDTOUnionid);
//
//        if(by != null&&code2Session.getUnionid()!=null){
//            return thirdPartyService.login(thirdPartyLoginDTOUnionid);
//        }
//        ThirdPartyLoginDTO thirdPartyLoginDTOOpenid = new ThirdPartyLoginDTO()
//                .setThirdPartyCode("wechat")
//                .setKey("openid")
//                .setValue(code2Session.getOpenid());
//        SysUserThirdParty by1 = thirdPartyService.get(thirdPartyLoginDTOOpenid);
//        if(by1 != null&&code2Session.getOpenid()!=null){
//            return thirdPartyService.login(thirdPartyLoginDTOOpenid);
//        }
//        SysUserThirdParty sysUserThirdParty = new SysUserThirdParty();
//        if(code2Session.getUnionid()!=null) {
//            sysUserThirdParty.setThirdPartyCode("wechat");
//            sysUserThirdParty.setKey("unionid");
//            sysUserThirdParty.setValue(code2Session.getUnionid());
//            return thirdPartyService.login(thirdPartyLoginDTOUnionid);
//        }
//        if(code2Session.getOpenid()!=null) {
//            sysUserThirdParty.setThirdPartyCode("wechat");
//            sysUserThirdParty.setKey("openid");
//            sysUserThirdParty.setValue(code2Session.getOpenid());
//            return thirdPartyService.login(thirdPartyLoginDTOOpenid);
//        }
//        throw FunctionalThirdPartyWechatException.LOGIN_FAIL;
        return null;
    }


    @SneakyThrows
    @Override
    public String getAccessToken() {
//        https://api.weixin.qq.com/cgi-bin/token
        String appid = setting.getWechatMpAppid();
        String appSecret = setting.getWechatMpAppSecret();
        String url = "https://api.weixin.qq.com/cgi-bin/token?" +
                "grant_type=client_credential" +
                "&grant_type=client_credential" +
                "&appid=" + appid + "&secret=" + appSecret;
        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url(url)
                .build();

        try (Response response = client.newCall(request).execute()) {
            String responseData = response.body().string();
            return JSONObject.parseObject(responseData).getString("access_token");
            // 处理响应数据
        } catch (IOException e) {
            // 处理异常
        }
        return null;
    }
}
